<template>
  <div>
    <!-- 筛选区域 -->
    <el-card>
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <!-- 表单 -->
      <el-form label-width="80px">
        <el-form-item label="状态：">
          <el-radio-group v-model="pageReq.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <my-channel @channelChange="channelChangeFn"></my-channel>
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker
            value-format="yyyy-MM-dd hh:mm:ss"
            v-model="dateArr"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="listArticle">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="margin-top:20px">
      <div slot="header">根据筛选条件共查询到 {{ total }} 条结果：</div>
      <!-- 表格 -->
    <el-table :data="articles">
      <el-table-column label="封面">
        <template slot-scope="scope">
          <el-image v-if="scope.row.cover.images[0]" :src="scope.row.cover.images[0]" style="width:150px;height:150px"></el-image>
          <img v-else src="../../assets/error.3f7b1f94.png"  width="200px" height="150px">
        </template>
      </el-table-column>
      <el-table-column label="标题" prop="title"></el-table-column>
      <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status===0" type="info">草稿</el-tag>
            <el-tag v-if="scope.row.status===1">待审核</el-tag>
            <el-tag v-if="scope.row.status===2" type="success">审核通过</el-tag>
            <el-tag v-if="scope.row.status===3" type="warning">审核失败</el-tag>
            <el-tag v-if="scope.row.status===4" type="danger">已删除</el-tag>
          </template>
        </el-table-column>
      <el-table-column label="发布时间" prop="pubdate"></el-table-column>
      <el-table-column label="阅读数" prop="read_count"></el-table-column>
      <el-table-column label="评论数" prop="comment_count"></el-table-column>
      <el-table-column label="点赞数" prop="like_count"></el-table-column>
      <el-table-column label="操作" width="150px">
        <template slot-scope="scope">
          <el-button style="margin-right:15px" type="primary" @click="editBtn(scope.row.id)" icon="el-icon-edit" circle></el-button>
          <el-button @click="delBtn(scope.row.id)" type="danger" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
    style="margin-top:20px"
      background
      layout="prev, pager, next"
      :current-page.sync="pageReq.page"
      :page-size="pageReq.per_page"
      @current-change="handleCurrentChange"
      :total="total">
    </el-pagination>
  </el-card>
  </div>
</template>

<script>
import { listArticleAPI, deleteArticleAPI } from '../../api/index.js'
import MyChannel from '../publish/components/my-channel.vue'
export default {
  name: 'articleIndex',
  components: { MyChannel },
  data () {
    return {
      // 频道选项
      channelOptions: [],
      // 日期范围 [起始时间，结束时间]
      dateArr: [],
      // 提交给后台的参数
      pageReq: {
        status: null,
        channel_id: null,
        begin_pubdate: null,
        end_pubdate: null,
        page: 1,
        per_page: 10
      },
      total: 0,
      articles: []
    }
  },
  created () {
    this.listArticle()
    // this.getchannelOptions()
  },
  methods: {
    channelChangeFn (channelId) {
      this.pageReq.channel_id = channelId
    },
    // 获取文章列表
    async listArticle () {
      try {
        if (this.dateArr instanceof Array) {
          const startDate = this.dateArr[0]
          if (startDate) {
            this.pageReq.begin_pubdate = startDate
          }
          const endDate = this.dateArr[1]
          if (endDate) {
            this.pageReq.end_pubdate = endDate
          }
        }
        const { data: res } = await listArticleAPI(this.pageReq)
        this.articles = res.data.results
        this.total = res.data.total_count
      } catch (error) {
        console.log(error)
      }
    },
    handleCurrentChange () {
      this.listArticle()
    },
    // 删除文章
    async delBtn (id) {
      try {
        await deleteArticleAPI(id)
        this.listArticle()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } catch (error) {
        console.log(error)
      }
      this.visible = false
    },
    // 编辑文章
    editBtn (id) {
      this.$router.push({
        name: 'publish',
        params: {
          id: id
        }
      })
    }
  }
}
</script>

<style>

</style>
